<?
	require_once('lib/OwnedObject.php');
	require_once('model/CarDeveloper.php');

	class CarModel extends OwnedObject {
		protected $modelName = 'CarModel';
		protected $developer='';
		protected $name='';


		public static function Install(){

			CarDeveloper::Install();

			$query=sprintf("CREATE TABLE `CarModel` (
							`Id` INT( 32 ) unsigned NOT NULL ,
							`Developer` INT( 32 ) unsigned NOT NULL ,
							`name` VARCHAR( 200 ) NOT NULL ,
							PRIMARY KEY ( `Id` ) ,
							UNIQUE (`Developer` ,`name`),
							FOREIGN KEY ( `Id` ) REFERENCES `Object`(`Id`) ON DELETE CASCADE,
							FOREIGN KEY (`Developer`) REFERENCES `CarDeveloper`(`Id`) ON DELETE CASCADE
							) ENGINE=INNODB DEFAULT CHARSET=utf8;
						");
			//file_put_contents('dump-CarModel.sql', $query);
			mysql_query($query);

			$query = sprintf("INSERT INTO `ObjectType` SET `name` = '%s'", strval(__CLASS__));
            mysql_query($query);
			echo "CarModel installed\n";
		}

		public function Load(){
			//echo '--'.$this->id.'--';
			parent::Load();

			$query=sprintf("SELECT `Developer`, `name`
							FROM `CarModel`
							WHERE `Id`=%d", $this->id);
			$row=mysql_query($query);
			$rez=mysql_fetch_array($row);

			$this->developer=$rez[0];
			$this->name=$rez[1];

		}

		public function AsSource($otherName='CarModel'){
			$rezult=parent::AsSource($otherName);


			$rezult->Attribute('name',$this->name);

			$Developer = new CarDeveloper($this->developer);
			$Developer->Load();
			$rezult -> MountSource($Developer->AsSource(),'Developer');


			return $rezult;
		}

		public function Developer($newDeveloper=false) {
                        if($newDeveloper){
                        	if($newDeveloper instanceof CarDeveloper)
                                $this->developer = $newDeveloper->Id();
                            else throw new Exception("Invalid argument CarDeveloper instance required");
						}
						$obj = new CarDeveloper($this->developer);
						$obj->Load();

						return $obj;
		}

		public function Name($newName=false) {
                        if($newName){
                                $this->name = $newName;
						}

                        return $this->name;
		}

		public function RegisterObject(){
						$obj_id= parent::RegisterObject();
						$query=sprintf("INSERT INTO
												`CarModel` ( `Id` , `Developer` , `name`)
											VALUES (%d, %d, '%s')
							", $obj_id, $this->developer, mes($this->name));
						mysql_query($query);
                        if(mysql_errno()) {
                                $this->Remove();
                                throw new ModelDuplicateException("Trying register exists model");
                        }
		}

        public function Save() {
                parent::Save();
                $query = sprintf("
                                UPDATE `CarModel`
                                SET
                                        `Developer`=%d,
                                        `name`='%s',
                                WHERE
                                        `Id`=%d",
                                mes($this->Developer), mes($this->name), $this->id
                        );
                mysql_query($query);
        }

		public function Remove(){
				$query=sprintf("DELETE FROM `CarModel` WHERE Id=%d",$this->id);
				mysql_query($query);
				parent::Remove();
		}
		
		public static function AllModels(){
		
			$query = "SELECT `Id` FROM `CarModel`";
			$parse = mysql_query($query);
		
			$models = new XmlSource('all-models');
			while($result = mysql_fetch_assoc($parse)){
				$item = new CarModel($result['Id']);
				$item->Load();
				$models->MountSource($item->AsSource());
			}
			
			return $models;
		
		}			

	}


    class ModelException extends Exception {
    }

    class ModelDuplicateException extends ModelException {
    }
?>